/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer.jxmap.render;

import java.awt.Graphics2D;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.LayerObject;
import nl.cloudfarming.client.geoviewer.ObjectLayer;
import nl.cloudfarming.client.geoviewer.Palette;
import nl.cloudfarming.client.geoviewer.RasterLayer;
import nl.cloudfarming.client.geoviewer.jxmap.map.LayerPanel;
import org.geotools.coverage.grid.GridCoverage2D;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.painter.Painter;
import org.netbeans.api.visual.model.ObjectState;

/**
 *
 * LayerPainter can be called from both a layerPanel directly in wich case the
 * argument will be a layerPanel (ie complies to Painter<LayerPanel>), but
 * also through a repaint of the main map, which is a jxmapviewer.
 * In the latter case the call complies to Painter<JXMapViewer> which will
 * case a classCastException.
 * The painters object is set to JXPanel as shared object between the two
 *
 * @author Timon Veenstra
 */
public class LayerPainter implements Painter<JXPanel> {

    private final Layer layer;

    public LayerPainter(Layer layer) {
        this.layer = layer;
    }

    @Override
    public void paint(Graphics2D graphics, JXPanel panel, int width, int height) {
        Graphics2D g = (Graphics2D) graphics.create();
        //convert from viewport to world bitmap
        if (panel instanceof LayerPanel) {
            LayerPanel layerPanel = (LayerPanel) panel;



            JXMapViewer map = layerPanel.getMapViewer();
            
            Palette palette = layerPanel.getLayer().getPalette();

            if (layerPanel.getLayer().isInterActive()) {
                g.setColor(palette.getColorForState(ObjectState.createNormal()));
            }

//        g.translate(-rect.x, -rect.y);

            if (layer instanceof ObjectLayer) {
                for (LayerObject lo : ((ObjectLayer<?>) layer).getObjects()) {
                    if (!layerPanel.getLayer().isInterActive()) {
                        g.setColor(palette.getColorForValue(lo.getKeyAttributeValue()));
                    }

                    GeometryRenderer renderer = GeometryRendererFactory.getRenderer(lo.getGeometry());
                    renderer.paint(lo.getGeometry(), map, g, false);
                }
            }
            if (layer instanceof RasterLayer) {
                GridCoverage2D raster = ((RasterLayer) layer).getRaster();
                RasterRenderer rasterRenderer = RasterRendererFactory.getRenderer(raster);
                rasterRenderer.paint(raster, map, g, this.layer.getPalette());
            }


        }

//        //create a polygon
//        Polygon poly = new Polygon();
//        for (GeoPosition gp : region) {
//            //convert geo to world bitmap pixel
//            Point2D pt = map.getTileFactory().geoToPixel(gp, map.getZoom());
//            poly.addPoint((int) pt.getX(), (int) pt.getY());
//        }
//
//        //do the drawing
//        g.setColor(new Color(255, 0, 0, 100));
//        g.fill(poly);
//        g.setColor(Color.RED);
//        g.draw(poly);
//
//        g.dispose();

    }
}
